﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AliFox.BaseService.BaseData.JobManagement.Dto;
using AliFox.BaseService.Permissions;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Linq;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;

namespace AliFox.BaseService.BaseData.JobManagement;

[Authorize(BaseServicePermissions.Job.Default)]
public class JobAppService(IRepository<Job, Guid> repository) : ApplicationService, IJobAppService
{
    public async Task<PagedResultDto<JobDto>> GetAll(GetJobInputDto input)
    {
        var query = (await repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), _ => _.Name.Contains(input.Filter));

        var totalCount = await query.CountAsync();
        var items = await query.OrderBy(input.Sorting ?? "Sort")
            .Skip(input.SkipCount)
            .Take(input.MaxResultCount)
            .ToListAsync();

        var dots = ObjectMapper.Map<List<Job>, List<JobDto>>(items);
        return new PagedResultDto<JobDto>(totalCount, dots);
    }

    public async Task<JobDto> Get(Guid id)
    {
        var result = await repository.GetAsync(id);
        return ObjectMapper.Map<Job, JobDto>(result);
    }

    [Authorize(BaseServicePermissions.Job.Create)]
    public async Task<JobDto> Create(CreateOrUpdateJobDto input)
    {
        var exist = await repository.FirstOrDefaultAsync(_ => _.Name == input.Name);
        if (exist != null)
        {
            throw new BusinessException("名称：" + input.Name + "岗位已存在");
        }

        var result = await repository.InsertAsync(new Job(GuidGenerator.Create(), CurrentTenant.Id, input.Name, input.Enabled, input.Sort, input.Description));
        return ObjectMapper.Map<Job, JobDto>(result);
    }

    [Authorize(BaseServicePermissions.Job.Update)]
    public async Task<JobDto> Update(Guid id, CreateOrUpdateJobDto input)
    {
        var job = await repository.GetAsync(id);

        job.Name = input.Name;
        job.Enabled = input.Enabled;
        job.Sort = input.Sort;
        job.Description = input.Description;

        await repository.UpdateAsync(job);

        return ObjectMapper.Map<Job, JobDto>(job);
    }

    [Authorize(BaseServicePermissions.Job.Delete)]
    public async Task Delete(List<Guid> ids)
    {
        foreach (var id in ids)
        {
            await repository.DeleteAsync(id);
        }
    }

    public async Task<ListResultDto<JobDto>> GetAllJobs()
    {
        var jobs = await repository.GetListAsync();

        return new ListResultDto<JobDto>(ObjectMapper.Map<List<Job>, List<JobDto>>(jobs));
    }
}